<?xml version='1.0' ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="functions.xslt"/>
  <xsl:output indent="yes" method="html" encoding="ISO-8859-1"/>
  <xsl:template match="/">
    <!--Starts applying the template on the first Group Node.-->
    <xsl:if test="/Report/Data/ResultSet/@RecordCount = 0">
      <xsl:apply-templates select="/Report/EmptyResult"  />
    </xsl:if>
    <xsl:if test="/Report/Data/ResultSet/@RecordCount > 0">
      <xsl:apply-templates select="/Report/Group"  />
    </xsl:if>

  </xsl:template>

  <xsl:template name="EmptyResultTemplate" match="EmptyResult">
    <div><xsl:value-of select="Message"/></div>
  </xsl:template>

  <!--Group Element. Main container to Reports.-->
  <xsl:template name="GroupTemplate" match="Group">
    <xsl:param name="CurrentGroup" select="self::node()"></xsl:param>
    <xsl:param name="CurrentGroupDataItems" select="//DataTable[@name=$CurrentGroup/@GroupByClass]/DataItems/DataItem"></xsl:param>
    <xsl:variable name="GroupByClass" select="$CurrentGroup/@GroupByClass"></xsl:variable>
    <xsl:for-each select="$CurrentGroupDataItems">
      <xsl:variable name="CurrentDataItem" select="self::node()"></xsl:variable>
      <table border="0" cellspacing="0" cellpadding="0" width="100%">
        <xsl:if test="$CurrentGroup/Title" >
          <tr>
            <td>
              <h1>
                <xsl:apply-templates select="$CurrentGroup/Title">
                  <xsl:with-param name="DataItemCollection" select="$CurrentDataItem" ></xsl:with-param>
                </xsl:apply-templates>
              </h1>
            </td>
          </tr>
        </xsl:if>
        <xsl:apply-templates select="$CurrentGroup/HeaderTemplate" ></xsl:apply-templates>
        <xsl:if test="name($CurrentDataItem) = 'DataItems'">
          <xsl:apply-templates select="$CurrentGroup/ItemTemplate" >
            <xsl:with-param name="DataItemsCollection" select="$CurrentDataItem/DataItem" ></xsl:with-param>
          </xsl:apply-templates>
        </xsl:if>
        <xsl:if test="name($CurrentDataItem) = 'DataItem'">
          <xsl:apply-templates select="$CurrentGroup/ItemTemplate" >
            <xsl:with-param name="DataItemsCollection" select="$CurrentDataItem" ></xsl:with-param>
          </xsl:apply-templates>
        </xsl:if>
        <xsl:if test="name($CurrentDataItem) = 'DataItems'">
          <xsl:apply-templates select="$CurrentGroup/FooterTemplate" >
            <xsl:with-param name="DataItemsCollection" select="$CurrentDataItem" ></xsl:with-param>
          </xsl:apply-templates>
        </xsl:if>
        <xsl:if test="name($CurrentDataItem) = 'DataItem'">
          <xsl:apply-templates select="$CurrentGroup/FooterTemplate" >
            <xsl:with-param name="DataItemsCollection" select="$CurrentDataItem/.." ></xsl:with-param>
          </xsl:apply-templates>
        </xsl:if>
      </table>
    </xsl:for-each>
  </xsl:template>

  <!--ItemTemplateElement. Interacts trough every DataItem with the same class as the GroupByClass of the ItemTemplate parent Group element.-->
  <xsl:template name="TemplateItemTemplate" match="ItemTemplate">
    <xsl:param name="DataItemsCollection" ></xsl:param>
    <xsl:param name="ColumnsCollection" select="Column"></xsl:param>
    <!--<xsl:variable name="GroupClass" select="parent::node()/@GroupByClass"></xsl:variable>-->
    <xsl:for-each select="$DataItemsCollection">
      <xsl:variable name="CurrentInnerItem" select="self::node()"></xsl:variable>
      <tr>
        <xsl:for-each select="$ColumnsCollection">
          <xsl:apply-templates select="self::node()">
            <xsl:with-param name="DataItem" select="$CurrentInnerItem" ></xsl:with-param>
          </xsl:apply-templates>
        </xsl:for-each>
      </tr>
    </xsl:for-each>
  </xsl:template>

  <!--Header Template-->
  <xsl:template name="TemplateHeaderTemplate" match="HeaderTemplate">
    <thead>
      <tr>
        <xsl:apply-templates select="Column">
          <xsl:with-param name="columnPlaceHolder" select="'th'"></xsl:with-param>
        </xsl:apply-templates>
      </tr>
    </thead>
  </xsl:template>

  <!--Footer Template. Receives a DataItems collectio to render summary info.-->
  <xsl:template name="TemplateFooterTemplate" match="FooterTemplate">
    <xsl:param name="DataItemsCollection" ></xsl:param>
    <xsl:param name="ColumnsCollection" select="Column"></xsl:param>
    <tr>
      <xsl:for-each select="$ColumnsCollection">
        <xsl:apply-templates select="self::node()">
          <xsl:with-param name="columnPlaceHolder" select="'th'"></xsl:with-param>
          <xsl:with-param name="DataItem" select="$DataItemsCollection" ></xsl:with-param>
        </xsl:apply-templates>
      </xsl:for-each>
    </tr>
  </xsl:template>

  <!--Title Tempate. Renders a Inner header to the grouping.-->
  <xsl:template name="TemplateTitle" match="Title">
    <xsl:param name="DataItemCollection" ></xsl:param>
    <xsl:for-each select="*">
      <xsl:if test="name()='Label'">
        <xsl:if test=".=''">
          <xsl:variable name="LabelClass" select="self::node()/@class" />
          <xsl:variable name="LabelField" select="self::node()/@fieldName" />

          <xsl:variable name="labelPath" select="$DataItemCollection/Field[@name=$LabelField][1]/child::node()"></xsl:variable>
          <xsl:value-of select="$labelPath"/>
        </xsl:if>
      </xsl:if>
      <xsl:if test="name()='Text'">
        <xsl:value-of select="."/>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <!--Column Template. Renders inner elements of a column.-->
  <xsl:template name="ColumnTemplate" match="Column">
    <xsl:param name="DataItem"></xsl:param>
    <xsl:param name="columnPlaceHolder" select="'td'"></xsl:param>

    <xsl:element name="{$columnPlaceHolder}">
      <xsl:attribute name="style">
        <xsl:if test="@Width">
          Width:<xsl:value-of select="@Width"/>;
        </xsl:if>
      </xsl:attribute>
      <xsl:attribute name="style">
        <xsl:if test="@style">
          <xsl:value-of select="@style"/>;
        </xsl:if>
      </xsl:attribute>
      <xsl:if test="Group">
        <xsl:variable name="GroupClass" select="./Group/@GroupByClass"></xsl:variable>
        <xsl:apply-templates select="Group">
          <xsl:with-param name="CurrentGroupDataItems" select="$DataItem/DataTable[@name=$GroupClass]/DataItems"></xsl:with-param>
        </xsl:apply-templates>
      </xsl:if>
      <xsl:apply-templates select="Text"></xsl:apply-templates>
      <xsl:apply-templates select="Field">
        <xsl:with-param name="DataItem" select="$DataItem"></xsl:with-param>
      </xsl:apply-templates>
      <xsl:apply-templates select="Sum">
        <xsl:with-param name="DataItems" select="$DataItem"></xsl:with-param>
      </xsl:apply-templates>
      <xsl:apply-templates select="Count">
        <xsl:with-param name="DataItems" select="$DataItem"></xsl:with-param>
      </xsl:apply-templates>
    </xsl:element>
  </xsl:template>

  <!--Field Template. Renders row level elements.-->
  <xsl:template name="FieldTemplate" match="Field">
    <xsl:param name="DataItem" ></xsl:param>
    <xsl:variable name="class" select="./@class"></xsl:variable>
    <xsl:variable name="name" select="./@value"></xsl:variable>
    <xsl:value-of select="string($DataItem/Field[@class=$class and @name=$name])"/>
  </xsl:template>

  <!--Text Template. Render info only.-->
  <xsl:template name="TemplateText" match="Text" >
    <div>
      <xsl:value-of select="@value"/>
    </div>
  </xsl:template>

  <!--#region Functions Templates-->
  <xsl:template name="funcSum" match="Sum">
    <xsl:param name="DataItems"></xsl:param>
    <xsl:if test="$DataItems">
      <xsl:variable name="class" select="./@class"></xsl:variable>
      <xsl:variable name="name" select="./@fieldName"></xsl:variable>
      <xsl:value-of select="sum($DataItems//Field[@class=$class and @name=$name])"/>
    </xsl:if>
  </xsl:template>
  <xsl:template name="funcCount" match="Count">
    <xsl:param name="DataItems"></xsl:param>
    <xsl:if test="$DataItems">
      <xsl:variable name="class" select="./@class"></xsl:variable>
      <xsl:variable name="name" select="./@fieldName"></xsl:variable>
      <xsl:value-of select="count($DataItems//Field[@class=$class and @name=$name])"/>
    </xsl:if>
  </xsl:template>
  <!--#endregion Functions Templates-->

</xsl:stylesheet>